/*
  Simple DirectMedia Layer
  Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"

#if SDL_VIDEO_DRIVER_WM

#include "SDL_wmevents.h"

#include "../../events/SDL_events_c.h"

void WM_MouseMotionEvent(SDL_WindowData *winData, int x, int y)
{    
    SDL_Window *window = winData->sdlwin;
    if (window) {
        SDL_SendMouseMotion(window, 0, 0, x, y);
    }
}

void WM_ButtonPressEvent(SDL_WindowData *winData, int btn)
{    
    SDL_Window *window = winData->sdlwin;
    if (window) {
        SDL_SendMouseButton(window, 0, SDL_PRESSED, btn);
    }
}

void WM_ButtonReleaseEvent(SDL_WindowData *winData, int btn)
{    
    SDL_Window *window = winData->sdlwin;
    if (window) {
        SDL_SendMouseButton(window, 0, SDL_RELEASED, btn);
    }
}

void WM_ButtonScrollEvent(SDL_WindowData *winData, int wheel)
{    
    SDL_Window *window = winData->sdlwin;
    if (window) {
        float x = 0.0f, y = 0.0f;
        switch (wheel) {
        case -1:
            y = 0 - 1.0f;
            break;
        case 1:
            y = 0 + 1.0f;
            break;
        default:
            break;
        }
        SDL_SendMouseWheel(window, 0, x, y, SDL_MOUSEWHEEL_NORMAL);
    }
}

uint32_t key_code_to_sdl_code(uint32_t code)
{
    switch (code) {
    case NX_KEY_A:
    case NX_KEY_a: return SDL_SCANCODE_A;
    case NX_KEY_B:
    case NX_KEY_b: return SDL_SCANCODE_B;
    case NX_KEY_C:
    case NX_KEY_c: return SDL_SCANCODE_C;
    case NX_KEY_D:
    case NX_KEY_d: return SDL_SCANCODE_D;
    case NX_KEY_E:
    case NX_KEY_e: return SDL_SCANCODE_E;
    case NX_KEY_F:
    case NX_KEY_f: return SDL_SCANCODE_F;
    case NX_KEY_G:
    case NX_KEY_g: return SDL_SCANCODE_G;
    case NX_KEY_H:
    case NX_KEY_h: return SDL_SCANCODE_H;
    case NX_KEY_I:
    case NX_KEY_i: return SDL_SCANCODE_I;
    case NX_KEY_J:
    case NX_KEY_j: return SDL_SCANCODE_J;
    case NX_KEY_K:
    case NX_KEY_k: return SDL_SCANCODE_K;
    case NX_KEY_L:
    case NX_KEY_l: return SDL_SCANCODE_L;
    case NX_KEY_M:
    case NX_KEY_m: return SDL_SCANCODE_M;
    case NX_KEY_N:
    case NX_KEY_n: return SDL_SCANCODE_N;
    case NX_KEY_O:
    case NX_KEY_o: return SDL_SCANCODE_O;
    case NX_KEY_P:
    case NX_KEY_p: return SDL_SCANCODE_P;
    case NX_KEY_Q:
    case NX_KEY_q: return SDL_SCANCODE_Q;
    case NX_KEY_R:
    case NX_KEY_r: return SDL_SCANCODE_R;
    case NX_KEY_S:
    case NX_KEY_s: return SDL_SCANCODE_S;
    case NX_KEY_T:
    case NX_KEY_t: return SDL_SCANCODE_T;
    case NX_KEY_U:
    case NX_KEY_u: return SDL_SCANCODE_U;
    case NX_KEY_V:
    case NX_KEY_v: return SDL_SCANCODE_V;
    case NX_KEY_W:
    case NX_KEY_w: return SDL_SCANCODE_W;
    case NX_KEY_X:
    case NX_KEY_x: return SDL_SCANCODE_X;
    case NX_KEY_Y:
    case NX_KEY_y: return SDL_SCANCODE_Y;
    case NX_KEY_Z:
    case NX_KEY_z: return SDL_SCANCODE_Z;
    case NX_KEY_EXCLAIM: /* ! */
    case NX_KEY_1: return SDL_SCANCODE_1;
    case NX_KEY_AT: /* @ */
    case NX_KEY_2: return SDL_SCANCODE_2;
    case NX_KEY_HASH: /* # */
    case NX_KEY_3: return SDL_SCANCODE_3;
    case NX_KEY_DOLLAR:  /* $ */
    case NX_KEY_4: return SDL_SCANCODE_4;
    case NX_KEY_PERSENT: /* % */
    case NX_KEY_5: return SDL_SCANCODE_5;
    case NX_KEY_CARET:   /* ^ */
    case NX_KEY_6: return SDL_SCANCODE_6;
    case NX_KEY_AMPERSAND:   /* & */
    case NX_KEY_7: return SDL_SCANCODE_7;
    case NX_KEY_ASTERISK:    /* * */
    case NX_KEY_8: return SDL_SCANCODE_8;
    case NX_KEY_LEFTPAREN: /* ( */
    case NX_KEY_9: return SDL_SCANCODE_9;
    case NX_KEY_RIGHTPAREN:  /* ) */
    case NX_KEY_0: return SDL_SCANCODE_0;
    case NX_KEY_ENTER: return SDL_SCANCODE_RETURN;
    case NX_KEY_ESCAPE: return SDL_SCANCODE_ESCAPE;
    case NX_KEY_BACKSPACE: return SDL_SCANCODE_BACKSPACE;
    case NX_KEY_TAB: return SDL_SCANCODE_TAB;
    case NX_KEY_SPACE: return SDL_SCANCODE_SPACE;

    case NX_KEY_UNDERSCRE:  /* _ */
    case NX_KEY_MINUS: return SDL_SCANCODE_MINUS;
    case NX_KEY_PLUS:    /* + */
    case NX_KEY_EQUALS: return SDL_SCANCODE_EQUALS;
    case NX_KEY_LEFTSQUAREBRACKET:
    case NX_KEY_LEFTBRACKET: return SDL_SCANCODE_LEFTBRACKET;
    case NX_KEY_RIGHTSQUAREBRACKET:
    case NX_KEY_RIGHTBRACKET: return SDL_SCANCODE_RIGHTBRACKET;
    case NX_KEY_VERTICAL:
    case NX_KEY_BACKSLASH: return SDL_SCANCODE_BACKSLASH;
    /* SDL_SCANCODE_NONUSHASH */
    case NX_KEY_COLON:   /* : */
    case NX_KEY_SEMICOLON: return SDL_SCANCODE_SEMICOLON;
    case NX_KEY_QUOTEDBL:    /* ' */
    case NX_KEY_QUOTE: return SDL_SCANCODE_APOSTROPHE;
    case NX_KEY_BACKQUOTE:   /* ` */
    case NX_KEY_TILDE: return SDL_SCANCODE_GRAVE;
    case NX_KEY_LESS:    /* < */
    case NX_KEY_COMMA: return SDL_SCANCODE_COMMA;
    case NX_KEY_GREATER: /* > */
    case NX_KEY_PERIOD: return SDL_SCANCODE_PERIOD;
    case NX_KEY_QUESTION:    /* ? */
    case NX_KEY_SLASH: return SDL_SCANCODE_SLASH;
    
    case NX_KEY_CAPSLOCK: return SDL_SCANCODE_CAPSLOCK;

    case NX_KEY_F1: return SDL_SCANCODE_F1;
    case NX_KEY_F2: return SDL_SCANCODE_F2;
    case NX_KEY_F3: return SDL_SCANCODE_F3;
    case NX_KEY_F4: return SDL_SCANCODE_F4;
    case NX_KEY_F5: return SDL_SCANCODE_F5;
    case NX_KEY_F6: return SDL_SCANCODE_F6;
    case NX_KEY_F7: return SDL_SCANCODE_F7;
    case NX_KEY_F8: return SDL_SCANCODE_F8;
    case NX_KEY_F9: return SDL_SCANCODE_F9;
    case NX_KEY_F10: return SDL_SCANCODE_F10;
    case NX_KEY_F11: return SDL_SCANCODE_F11;
    case NX_KEY_F12: return SDL_SCANCODE_F12;

    case NX_KEY_PRINT: return SDL_SCANCODE_PRINTSCREEN;
    case NX_KEY_SCROLLOCK: return SDL_SCANCODE_SCROLLLOCK;
    case NX_KEY_PAUSE: return SDL_SCANCODE_PAUSE;
    case NX_KEY_INSERT: return SDL_SCANCODE_INSERT;
    case NX_KEY_HOME: return SDL_SCANCODE_HOME;
    case NX_KEY_PAGEUP: return SDL_SCANCODE_PAGEUP;
    case NX_KEY_DELETE: return SDL_SCANCODE_DELETE;
    case NX_KEY_END: return SDL_SCANCODE_END;
    case NX_KEY_PAGEDOWN: return SDL_SCANCODE_PAGEDOWN;
    case NX_KEY_RIGHT: return SDL_SCANCODE_RIGHT;
    case NX_KEY_LEFT: return SDL_SCANCODE_LEFT;
    case NX_KEY_DOWN: return SDL_SCANCODE_DOWN;
    case NX_KEY_UP: return SDL_SCANCODE_UP;

    case NX_KEY_NUMLOCK: return SDL_SCANCODE_NUMLOCKCLEAR;
    case NX_KEY_KP_DIVIDE: return SDL_SCANCODE_KP_DIVIDE;
    case NX_KEY_KP_MULTIPLY: return SDL_SCANCODE_KP_MULTIPLY;
    case NX_KEY_KP_MINUS: return SDL_SCANCODE_KP_MINUS;
    case NX_KEY_KP_PLUS: return SDL_SCANCODE_KP_PLUS;
    case NX_KEY_KP_ENTER: return SDL_SCANCODE_KP_ENTER;
    case NX_KEY_KP1: return SDL_SCANCODE_KP_1;
    case NX_KEY_KP2: return SDL_SCANCODE_KP_2;
    case NX_KEY_KP3: return SDL_SCANCODE_KP_3;
    case NX_KEY_KP4: return SDL_SCANCODE_KP_4;
    case NX_KEY_KP5: return SDL_SCANCODE_KP_5;
    case NX_KEY_KP6: return SDL_SCANCODE_KP_6;
    case NX_KEY_KP7: return SDL_SCANCODE_KP_7;
    case NX_KEY_KP8: return SDL_SCANCODE_KP_8;
    case NX_KEY_KP9: return SDL_SCANCODE_KP_9;
    case NX_KEY_KP0: return SDL_SCANCODE_KP_0;
    case NX_KEY_KP_PERIOD: return SDL_SCANCODE_KP_PERIOD;
    /* SDL_SCANCODE_NONUSBACKSLASH */
    /* SDL_SCANCODE_APPLICATION */
    case NX_KEY_POWER: return SDL_SCANCODE_POWER;
    
    case NX_KEY_KP_EQUALS: return SDL_SCANCODE_KP_EQUALS;
    case NX_KEY_F13: return SDL_SCANCODE_F13;
    case NX_KEY_F14: return SDL_SCANCODE_F14;
    case NX_KEY_F15: return SDL_SCANCODE_F15;
    /* F16~F24 */
    /* SDL_SCANCODE_EXECUTE */
    case NX_KEY_HELP: return SDL_SCANCODE_HELP;
    case NX_KEY_MENU: return SDL_SCANCODE_MENU;
    /* SDL_SCANCODE_SELECT */
    case NX_KEY_EURO: return SDL_SCANCODE_AGAIN; /* REDO */
    case NX_KEY_UNDO: return SDL_SCANCODE_UNDO;
    /*
    SDL_SCANCODE_CUT = 123,
    SDL_SCANCODE_COPY = 124,
    SDL_SCANCODE_PASTE = 125,
    SDL_SCANCODE_FIND = 126,
    SDL_SCANCODE_MUTE = 127,
    SDL_SCANCODE_VOLUMEUP = 128,
    SDL_SCANCODE_VOLUMEDOWN = 129,
    SDL_SCANCODE_KP_COMMA = 133,
    SDL_SCANCODE_KP_EQUALSAS400 = 134,
    */
    #if 0
    SDL_SCANCODE_INTERNATIONAL1 = 135, /**< used on Asian keyboards, see
                                            footnotes in USB doc */
    SDL_SCANCODE_INTERNATIONAL2 = 136,
    SDL_SCANCODE_INTERNATIONAL3 = 137, /**< Yen */
    SDL_SCANCODE_INTERNATIONAL4 = 138,
    SDL_SCANCODE_INTERNATIONAL5 = 139,
    SDL_SCANCODE_INTERNATIONAL6 = 140,
    SDL_SCANCODE_INTERNATIONAL7 = 141,
    SDL_SCANCODE_INTERNATIONAL8 = 142,
    SDL_SCANCODE_INTERNATIONAL9 = 143,
    SDL_SCANCODE_LANG1 = 144, /**< Hangul/English toggle */
    SDL_SCANCODE_LANG2 = 145, /**< Hanja conversion */
    SDL_SCANCODE_LANG3 = 146, /**< Katakana */
    SDL_SCANCODE_LANG4 = 147, /**< Hiragana */
    SDL_SCANCODE_LANG5 = 148, /**< Zenkaku/Hankaku */
    SDL_SCANCODE_LANG6 = 149, /**< reserved */
    SDL_SCANCODE_LANG7 = 150, /**< reserved */
    SDL_SCANCODE_LANG8 = 151, /**< reserved */
    SDL_SCANCODE_LANG9 = 152, /**< reserved */

    SDL_SCANCODE_ALTERASE = 153, /**< Erase-Eaze */
    #endif
    case NX_KEY_SYSREQ: return SDL_SCANCODE_SYSREQ;
    /* SDL_SCANCODE_CANCEL */
    case NX_KEY_CLEAR: return SDL_SCANCODE_CLEAR;
    /*
    SDL_SCANCODE_PRIOR = 157,
    SDL_SCANCODE_RETURN2 = 158,
    SDL_SCANCODE_SEPARATOR = 159,
    SDL_SCANCODE_OUT = 160,
    SDL_SCANCODE_OPER = 161,
    SDL_SCANCODE_CLEARAGAIN = 162,
    SDL_SCANCODE_CRSEL = 163,
    SDL_SCANCODE_EXSEL = 164,
    
    SDL_SCANCODE_KP_00 = 176,
    SDL_SCANCODE_KP_000 = 177,
    SDL_SCANCODE_THOUSANDSSEPARATOR = 178,
    SDL_SCANCODE_DECIMALSEPARATOR = 179,
    SDL_SCANCODE_CURRENCYUNIT = 180,
    SDL_SCANCODE_CURRENCYSUBUNIT = 181,
    SDL_SCANCODE_KP_LEFTPAREN = 182,
    SDL_SCANCODE_KP_RIGHTPAREN = 183,
    SDL_SCANCODE_KP_LEFTBRACE = 184,
    SDL_SCANCODE_KP_RIGHTBRACE = 185,
    SDL_SCANCODE_KP_TAB = 186,
    SDL_SCANCODE_KP_BACKSPACE = 187,
    SDL_SCANCODE_KP_A = 188,
    SDL_SCANCODE_KP_B = 189,
    SDL_SCANCODE_KP_C = 190,
    SDL_SCANCODE_KP_D = 191,
    SDL_SCANCODE_KP_E = 192,
    SDL_SCANCODE_KP_F = 193,
    SDL_SCANCODE_KP_XOR = 194,
    SDL_SCANCODE_KP_POWER = 195,
    SDL_SCANCODE_KP_PERCENT = 196,
    SDL_SCANCODE_KP_LESS = 197,
    SDL_SCANCODE_KP_GREATER = 198,
    SDL_SCANCODE_KP_AMPERSAND = 199,
    SDL_SCANCODE_KP_DBLAMPERSAND = 200,
    SDL_SCANCODE_KP_VERTICALBAR = 201,
    SDL_SCANCODE_KP_DBLVERTICALBAR = 202,
    SDL_SCANCODE_KP_COLON = 203,
    SDL_SCANCODE_KP_HASH = 204,
    SDL_SCANCODE_KP_SPACE = 205,
    SDL_SCANCODE_KP_AT = 206,
    SDL_SCANCODE_KP_EXCLAM = 207,
    SDL_SCANCODE_KP_MEMSTORE = 208,
    SDL_SCANCODE_KP_MEMRECALL = 209,
    SDL_SCANCODE_KP_MEMCLEAR = 210,
    SDL_SCANCODE_KP_MEMADD = 211,
    SDL_SCANCODE_KP_MEMSUBTRACT = 212,
    SDL_SCANCODE_KP_MEMMULTIPLY = 213,
    SDL_SCANCODE_KP_MEMDIVIDE = 214,
    SDL_SCANCODE_KP_PLUSMINUS = 215,
    SDL_SCANCODE_KP_CLEAR = 216,
    SDL_SCANCODE_KP_CLEARENTRY = 217,
    SDL_SCANCODE_KP_BINARY = 218,
    SDL_SCANCODE_KP_OCTAL = 219,
    SDL_SCANCODE_KP_DECIMAL = 220,
    SDL_SCANCODE_KP_HEXADECIMAL = 221,
    */
    case NX_KEY_LCTRL: return SDL_SCANCODE_LCTRL;
    case NX_KEY_LSHIFT: return SDL_SCANCODE_LSHIFT;
    case NX_KEY_LALT: return SDL_SCANCODE_LALT;
    /* SDL_SCANCODE_LGUI */
    case NX_KEY_RCTRL: return SDL_SCANCODE_RCTRL;
    case NX_KEY_RSHIFT: return SDL_SCANCODE_RSHIFT;
    case NX_KEY_RALT: return SDL_SCANCODE_RALT;
    /* SDL_SCANCODE_RGUI */
    case NX_KEY_MODE: return SDL_SCANCODE_MODE;
    /*
    SDL_SCANCODE_AUDIONEXT = 258,
    SDL_SCANCODE_AUDIOPREV = 259,
    SDL_SCANCODE_AUDIOSTOP = 260,
    SDL_SCANCODE_AUDIOPLAY = 261,
    SDL_SCANCODE_AUDIOMUTE = 262,
    SDL_SCANCODE_MEDIASELECT = 263,
    SDL_SCANCODE_WWW = 264,
    SDL_SCANCODE_MAIL = 265,
    SDL_SCANCODE_CALCULATOR = 266,
    SDL_SCANCODE_COMPUTER = 267,
    SDL_SCANCODE_AC_SEARCH = 268,
    SDL_SCANCODE_AC_HOME = 269,
    SDL_SCANCODE_AC_BACK = 270,
    SDL_SCANCODE_AC_FORWARD = 271,
    SDL_SCANCODE_AC_STOP = 272,
    SDL_SCANCODE_AC_REFRESH = 273,
    SDL_SCANCODE_AC_BOOKMARKS = 274,
    */
    #if 0
    SDL_SCANCODE_BRIGHTNESSDOWN = 275,
    SDL_SCANCODE_BRIGHTNESSUP = 276,
    SDL_SCANCODE_DISPLAYSWITCH = 277, /**< display mirroring/dual display
                                           switch, video mode switch */
    SDL_SCANCODE_KBDILLUMTOGGLE = 278,
    SDL_SCANCODE_KBDILLUMDOWN = 279,
    SDL_SCANCODE_KBDILLUMUP = 280,
    SDL_SCANCODE_EJECT = 281,
    SDL_SCANCODE_SLEEP = 282,

    SDL_SCANCODE_APP1 = 283,
    SDL_SCANCODE_APP2 = 284,

    SDL_SCANCODE_AUDIOREWIND = 285,
    SDL_SCANCODE_AUDIOFASTFORWARD = 286,
    #endif
    default: break;
    }
    return SDL_SCANCODE_UNKNOWN;
}

int wm_keypad2ascii(int code)
{
    if (code >= NX_KEY_KP0 && code <= NX_KEY_KP_PLUS) {
        switch (code) {
        case NX_KEY_KP0:
            return '0';
        case NX_KEY_KP1:
            return '1';
        case NX_KEY_KP2:
            return '2';            
        case NX_KEY_KP3:
            return '3';
        case NX_KEY_KP4:
            return '4';
        case NX_KEY_KP5:
            return '5';            
        case NX_KEY_KP6:
            return '6';
        case NX_KEY_KP7:
            return '7';
        case NX_KEY_KP8:
            return '8';            
        case NX_KEY_KP9:
            return '9';
        case NX_KEY_KP_PERIOD:
            return '.';
        case NX_KEY_KP_DIVIDE:
            return '/';            
        case NX_KEY_KP_MULTIPLY:
            return '*';
        case NX_KEY_KP_MINUS:
            return '-';
        case NX_KEY_KP_PLUS:
            return '+';
        default:
            break;
        }
    }
    return code;
}

SDL_bool SDL_SacncodeVisiable(uint32_t scancode)
{
    if ((scancode >= SDL_SCANCODE_A && scancode <= SDL_SCANCODE_0)
    || (scancode == SDL_SCANCODE_SPACE)
    || (scancode >= SDL_SCANCODE_MINUS && scancode <= SDL_SCANCODE_SLASH)
    || (scancode >= SDL_SCANCODE_KP_DIVIDE && scancode <= SDL_SCANCODE_KP_PERIOD))
        return SDL_TRUE;
    return SDL_FALSE;
}

/**
 * 如果打开了文本输入：
 * 如果按下了组合键，那么就发送组合键+Key中的key。
 * 没有按下组合键，如果是可显示按键，那么就发送成为SDL_TEXTINPUT事件
 * 如果不是可显示按键，就直接发送该按键。
 */
void WM_KeyPressEvent(SDL_WindowData *winData, int keycode, int keymod)
{    
    if (winData) {
        uint32_t scancode = key_code_to_sdl_code(keycode);
        if (winData->deviceData->startTextInput) {
            if (keymod & (NX_KMOD_CTRL | NX_KMOD_ALT)) {    /* 有组合按键，就直接转发按键 */
                if (scancode != SDL_SCANCODE_UNKNOWN)
                    SDL_SendKeyboardKey(SDL_PRESSED, scancode);   
            } else {    /* 没有组合按键，可显示字符就发送给文本，不可显示就直接发送按键值 */
                if (SDL_SacncodeVisiable(scancode) && scancode != SDL_SCANCODE_UNKNOWN) {
                    /* 生产文本字符，产生SDL_TEXTINPUT消息 */
                    char s[2] = {0};
                    s[0] = wm_keypad2ascii(keycode); /* 可显示按键转ASCII */
                    if (s[0])
                        SDL_SendKeyboardText(s);
                } else {
                    if (scancode != SDL_SCANCODE_UNKNOWN)
                        SDL_SendKeyboardKey(SDL_PRESSED, scancode);
                }
            }
        } else {    /* 没有开启文本输入，是什么按键就转发什么按键 */
            if (scancode != SDL_SCANCODE_UNKNOWN)
                SDL_SendKeyboardKey(SDL_PRESSED, scancode);   
        }
    }
}

/**
 * 如果打开了文本输入：
 * 如果弹起了组合键，那么就发送组合键+Key中的key。
 * 没有按下组合键，如果是可显示按键，那么忽略不管
 * 如果不是可显示按键，就直接发送该按键。
 */
void WM_KeyReleaseEvent(SDL_WindowData *winData, int keycode, int keymod)
{    
    if (winData) {
        uint32_t scancode = key_code_to_sdl_code(keycode);
        if (winData->deviceData->startTextInput) {
            if (keymod & (NX_KMOD_SHIFT | NX_KMOD_CTRL | NX_KMOD_ALT)) {    /* 有组合按键，就直接转发按键 */
                if (scancode != SDL_SCANCODE_UNKNOWN)
                    SDL_SendKeyboardKey(SDL_RELEASED, scancode);   
            } else {    /* 没有组合按键，不可显示就直接发送按键值 */
                if (!SDL_SacncodeVisiable(scancode)) {
                    if (scancode != SDL_SCANCODE_UNKNOWN)
                        SDL_SendKeyboardKey(SDL_RELEASED, scancode);
                }
            }
        } else {    /* 没有开启文本输入，是什么按键就转发什么按键 */
            if (scancode != SDL_SCANCODE_UNKNOWN)
                SDL_SendKeyboardKey(SDL_RELEASED, scancode);   
        }
    }
}

/* has event return 0, or not return -1 */
int wm_poll_event(SDL_WindowData * data)
{
    NX_WindowEvent e;
    if (NX_WindowPollEvent(data->window, &e) == NX_EOK)
    {
        switch (e.type)
        {
        case NX_KEYDOWN_EVENT:
            WM_KeyPressEvent(data, e.keydown_event.code, e.keydown_event.modify);
            break;
        case NX_KEYUP_EVENT:
            WM_KeyReleaseEvent(data, e.keyup_event.code, e.keydown_event.modify);
            break;
        case NX_MOUSEWHEELUP_EVENT:
            WM_ButtonScrollEvent(data, -1);
            break;
        case NX_MOUSEWHEELDOWN_EVENT:
            WM_ButtonScrollEvent(data, 1);
            break;
        case NX_MOUSEBUTTONDOWN_EVENT:
            switch(e.mouse_buttondown_event.code)
            {
            case NX_BTN_MOUSE_LEFT:
                WM_ButtonPressEvent(data, SDL_BUTTON_LEFT);
                break;
            case NX_BTN_MOUSE_MIDDLE:
                WM_ButtonPressEvent(data, SDL_BUTTON_MIDDLE);
                break;
            case NX_BTN_MOUSE_RIGHT:
                WM_ButtonPressEvent(data, SDL_BUTTON_RIGHT);
                break;
            }
            break;
        case NX_MOUSEBUTTONUP_EVENT:
            switch(e.mouse_buttonup_event.code)
            {
            case NX_BTN_MOUSE_LEFT:
                WM_ButtonReleaseEvent(data, SDL_BUTTON_LEFT);
                break;
            case NX_BTN_MOUSE_MIDDLE:
                WM_ButtonReleaseEvent(data, SDL_BUTTON_MIDDLE);
                break;
            case NX_BTN_MOUSE_RIGHT:
                WM_ButtonReleaseEvent(data, SDL_BUTTON_RIGHT);
                break;
            }
            break;
        case NX_ABS_EVENT:
            WM_MouseMotionEvent(data, e.abs_event.x, e.abs_event.y);
            break;
        default:
            break;
        }
        return 0;
    }
    return -1;
}

/**
 * Runs the main event loop.
 * @param   _THIS
 */
void WM_PumpEvents(_THIS)
{
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    SDL_WindowData * winData = data->winData;

    wm_poll_event(winData);
}


#endif /* SDL_VIDEO_DRIVER_WM */

/* vi: set ts=4 sw=4 expandtab: */
